package com.zxmr.client;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.ShutdownSignalException;

public class ClientResponse {
	
	private final static String QUEUE_NAME = "response";
	
	public static void main(String[] args) throws java.io.IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException{
		// TODO Auto-generated method stub
		ConnectionFactory factory = new ConnectionFactory();
	   // factory.setHost("localhost");
		factory.setVirtualHost("/");
	    Connection connection = factory.newConnection();
	    Channel channel = connection.createChannel();
	    
	    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
	    
	    QueueingConsumer consumer = new QueueingConsumer(channel);
	    channel.basicConsume(QUEUE_NAME, true, consumer);


	    while (true) {
	      QueueingConsumer.Delivery delivery = consumer.nextDelivery();
	      
	        AMQP.BasicProperties requestProperties = delivery.getProperties();
	        String correlationId = requestProperties.getCorrelationId();
	        String replyTo = requestProperties.getReplyTo();
	        
	      System.out.println("source routing key is "+replyTo+" correlation id is "+correlationId);
	      String message = new String(delivery.getBody());
	      System.out.println(" [x] Received '" + message + "'");
	    }
	}
}
